﻿using Hx.ADSyncPlatform.Entity.Entities;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;

namespace Hx.ADSyncPlatform.User
{
    public class PlatManager : IUser
    {
        private readonly IHttpContextAccessor _accessor;

        private readonly Infrastructure.Cache.ICacheService cacheService;

        public PlatManager(IHttpContextAccessor accessor, Infrastructure.Cache.ICacheService cacheService)
        {
            _accessor = accessor;
            this.cacheService = cacheService;
        }

        public string Name => _accessor.HttpContext.User.Identity.Name;
        public int ID => GetClaimValueByType(ClaimTypes.NameIdentifier).FirstOrDefault().ObjToInt();

        public Guid ManagerId => Guid.Parse(GetClaimValueByType(JwtRegisteredClaimNames.Jti).FirstOrDefault());

        public bool IsAuthenticated()
        {
            return _accessor.HttpContext.User.Identity.IsAuthenticated;
        }


        public string GetToken()
        {
            return _accessor.HttpContext.Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
        }

        public List<string> GetUserInfoFromToken(string ClaimType)
        {

            var jwtHandler = new JwtSecurityTokenHandler();
            if (!string.IsNullOrEmpty(GetToken()))
            {
                JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(GetToken());

                return (from item in jwtToken.Claims
                        where item.Type == ClaimType
                        select item.Value).ToList();
            }
            else
            {
                return new List<string>() { };
            }
        }

        public IEnumerable<Claim> GetClaimsIdentity()
        {
            return _accessor.HttpContext.User.Claims;
        }

        public List<string> GetClaimValueByType(string ClaimType)
        {

            return (from item in GetClaimsIdentity()
                    where item.Type == ClaimType
                    select item.Value).ToList();

        }


        public string DataPermissionCacheKey => this.IsAuthenticated() ? this.ManagerId.ToString() + "-" + "DATAPERMISSION" : "";

        /// <summary>
        /// AD组织数据权限
        /// </summary>
        public List<Plat_DataPermission> DataPermissions
        {
            get
            {
                if (this.IsAuthenticated())
                {
                    if (cacheService.Exists(DataPermissionCacheKey))
                    {
                        return cacheService.Get<List<Plat_DataPermission>>(DataPermissionCacheKey);
                    }
                }
                return new List<Plat_DataPermission>();

            }
        }

        public string LoginType
        {
            get
            {
                List<string> loginTypes = this.GetClaimValueByType("loginType");
                return loginTypes != null && loginTypes.Any() ? loginTypes[0] : "";
            }
        }
    }
}
